package nio.muban;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.concurrent.TimeUnit;

public class MySocketClient {

    public static void main(String[] args) throws IOException, InterruptedException {

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        socketChannel.connect(new InetSocketAddress("127.0.0.1",8088));

        if (socketChannel.finishConnect()) {
            int i=0;
            while(true) {
                TimeUnit.SECONDS.sleep(1);
                String string =  "I'm "+i+++"-th information from client";
                buffer.clear();
                buffer.put(string.getBytes());
                for(int j=0; j<buffer.position(); j++) {

                    System.out.print((char)buffer.get(j));
                }
                System.out.println();
                buffer.flip();
                while(buffer.hasRemaining()) {
                    socketChannel.write(buffer);
                }
            }



        }
        socketChannel.close();
    }

}
